package cn.com.gridinfo.sooc.base;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.base.ISetupToolBar;
import com.jeremy.arad.base.ToolBarActivity;
import com.jeremy.arad.widget.dialog.ProgressHUD;

import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 基础 fragment 继承框架里 basefragment
 * @author jieli
 */
public class BaseSoocFragment extends Fragment implements ISetupToolBar,SoocNetResult {


    private TextView mTitle;
    private ImageView mLeftButton;
    private ImageView mRightButton;

    ProgressHUD mProgressHUD;
    protected BaseSoocActivity baseActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        baseActivity = (BaseSoocActivity) context;
    }

    @Override
    public void onResume() {
        super.onResume();
        FragmentActivity parent = getActivity();
        if (getParentFragment() == null && parent instanceof ToolBarActivity) {
            if (setupToolBarTitle() != null) {

                View view = parent.getWindow().getDecorView();

                mTitle = (TextView) view.findViewById(com.jeremy.arad.R.id.toolbar_title);
                mLeftButton = (ImageView) view.findViewById(com.jeremy.arad.R.id.toolbar_leftbtn);
                mRightButton = (ImageView) view.findViewById(com.jeremy.arad.R.id.toolbar_rightbtn);

                if (mTitle != null && setupToolBarTitle() != null)
                    mTitle.setText(setupToolBarTitle());

                if (mLeftButton != null) {
                    if (setupToolBarLeftButton(mLeftButton)) {
                        mLeftButton.setVisibility(View.VISIBLE);
                    } else {
                        mLeftButton.setVisibility(View.GONE);
                    }
                }

                if (mRightButton != null) {
                    if (setupToolBarRightButton(mRightButton)) {
                        mRightButton.setVisibility(View.VISIBLE);
                    } else {
                        mRightButton.setVisibility(View.GONE);
                    }
                }

            }

        }
    }


    @Override
    public ImageView getmRightButton() {
        return mRightButton;
    }

    @Override
    public TextView getmTitle() {
        return mTitle;
    }

    @Override
    public ImageView getmLeftButton() {
        return mLeftButton;
    }

    @Override
    public String setupToolBarTitle() {
        return null;
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return false;
    }

    @Override
    public boolean setupToolBarRightButton(ImageView rightButton) {
        return false;
    }


    /**
     * 请求成功
     * @param requestCode 网络请求顺序号，第一个请求，NetRequestOrderNum=0,处理第一条请求的结果。如果等于1,
     */
    @Override
    public void onRequestSuccess(int requestCode) {

    }

    /**
     * 请求失败
     * @param requestCode 请求链接 code
     * @param errorNo  请求网络失败 code 如404等
     * @param errorMessage  请求网络错误信息
     */
    @Override
    public void onRequestFaild(int requestCode,String errorNo, String errorMessage) {
        //两种进度加载方式，点击空白区域进度条是否取消
        showProgress(false);
        showProgress(false,false);
        MessageUtils.showImageShortToast(baseActivity, errorMessage);
    }

    /**
     * 未登录处理
     */
    @Override
    public void onInvalidLogin() {
//        MessageUtils.showShortToast(baseActivity,"请登录");
//        IntentUtil.intentPushUp(baseActivity, LoginActivity.class,00);
//        AlertDialog.Builder bl = new AlertDialog.Builder(baseActivity)
//                .setMessage("请登陆")
//                .setPositiveButton(R.string.app_name,new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
////                        ScoreApplication.getContext().restartApp();
//                    }
//                })
//                .setCancelable(false);
//        bl.create().show();
//
    }

    /**
     * 无网络处理
     */
    @Override
    public void onNoConnect() {
        showProgress(false);
        showProgress(false, false);
        MessageUtils.showImageShortToast(baseActivity, "无网络连接");
    }

    /**
     * 进度加载 点击可取消加载进度条
     * @param show
     */
    public void showProgress(boolean show) {
        showProgress(show,false);
    }

    /**
     * 进度加载 点击可取消加载进度条
     * @param show
     * @param isCancle 点击是否可取消进度加载
     */
    public void showProgress(boolean show,boolean isCancle) {
        showProgressWithText(show, "加载中...",isCancle);
    }

    public void showProgressWithText(boolean show, String message,boolean isCancle) {

        if (show) {
            mProgressHUD = ProgressHUD.show(getContext(), message, true, isCancle, null);
        } else {
            if (mProgressHUD != null) {
                mProgressHUD.dismiss();
            }
        }
    }
}
